//
//  HomeViewController.swift
//  quizDemo
//
//  Created by LiuXing on 2017/9/9.
//  Copyright © 2017年 quiz. All rights reserved.
//

import UIKit
import SDCycleScrollView
import SnapKit

private let reuseIdentifier = "HomeCollectionViewCell"
class HomeViewController: UIViewController {
    
    var itemArray: [HomeModel]? = [HomeModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        setupData()
    }
    
    func setupUI() {
//        title = "EZGames"
        view.backgroundColor = UIColor.black
        view.addSubview(bannerView!)
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(bannerView!.snp.bottom).offset(20)
        }
    }
    func setupData() {
        let model = setupHomeModel(text: "Register", color: UIColor.red)
        itemArray?.append(model)
        itemArray?.append(setupHomeModel(text: "Cart", color: UIColor.orange))
        itemArray?.append(setupHomeModel(text: "News&Bounes", color: UIColor.blue))
        itemArray?.append(setupHomeModel(text: "Support", color: UIColor.brown))
        collectionView.reloadData()
    }
    
    func setupHomeModel(text: String?, color: UIColor?) -> HomeModel {
        let model = HomeModel.init()
        model.itemColor = color
        model.itemText = text
        return model
    }
    
    private lazy var bannerView: SDCycleScrollView? = {
        let bannerView = SDCycleScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200), delegate: self, placeholderImage: UIImage.init(named: "placeholder"))
        bannerView?.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter
        bannerView?.imageURLStringsGroup = self.imagesURLStrings
        return bannerView
    }()
    
    private lazy var imagesURLStrings: [String?] = [
    "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
    "https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
    "http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
    ]
    
    private lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.flowLayout)
        collectionView.backgroundColor = UIColor.black
        collectionView.showsHorizontalScrollIndicator = false
        // Register cell
        collectionView.register(UINib.init(nibName: "HomeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.dataSource = self
        collectionView.delegate = self
        return collectionView
    }()
    
    private lazy var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 10
        flowLayout.itemSize = CGSize(width: (ScreenWidth)/2, height: (ScreenWidth - 80)/2)
        return flowLayout
    }()
}

extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return itemArray?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HomeCollectionViewCell
        let model = itemArray?[indexPath.item]
        cell.model = model
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 1 {
            let cart = LZCartViewController()
            present(cart, animated: true, completion: nil)
        } else if indexPath.item == 0 {
            let vc = LoginViewController()
            vc.isLogin = false
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}

extension HomeViewController: SDCycleScrollViewDelegate {
}
